const PublicModel = require("@/common/public.model");
const { TenantContext } = require("@/core/tenant-context");

/**
 * 桌台
 */
class TableModel extends PublicModel {
    /**
     * 查询桌台列表
     */
    queryTableList({ keyword }) {
        const SQL = `
            SELECT
                t.id,
                t.area_id,
                t.current_order_id,
                t.features,
                t.status,
                t.table_code,
                t.capacity,
                t.position,
                t.table_name,
                ta.feature_tags area_feature_tags,
                ta.floor area_floor,
                ta.area_code,
                ta.area_name,
                ta.area_type,
                ta.remark area_remark,
                ta.status
            FROM
                game_tables t
                LEFT JOIN game_table_areas ta ON t.area_id = ta.id
            WHERE
                t.table_name LIKE '%${keyword || ''}%'
                AND t.is_delete = 0
                AND ta.is_delete = 0
                AND t.tenant_id = '${TenantContext.getCurrentTenant()}'
                AND ta.tenant_id = '${TenantContext.getCurrentTenant()}' 
            LIMIT 50
        `;
        return this.query(SQL);
    }

    /**
     * 查询桌台区域表
     */
    queryTableAreaList() {
        const SQL = `
            SELECT
                area_name,
                area_code,
                area_type,
                floor,
                feature_tags,
                remark,
                sort_order
            FROM
                game_table_areas 
            WHERE 
                is_delete = 0
            LIMIT 50
        `;
        return this.query(SQL);

    }

    /**
     * 创建桌台
     * @param {*} data 
     * @returns 
     */
    createTable(data) {
        return this.create({ table: 'game_tables', data: data, result: true });
    }

    /**
     * 创建桌台房间
     * @param {*} data 
     * @returns 
     */
    createTableRoom(data) {
        return this.create({ table: 'game_rooms', data: data, result: true });
    }

    /**
     * 创建桌台区域
     * @param {*} data 
     * @returns 
     */
    createTableArea(data) {
        return this.create({ table: 'game_table_areas', data: data, result: true });
    }

    /**
     * 创建桌台规则
     * @param {*} data 
     * @returns 
     */
    createRule(data) {
        return this.create({ table: 'game_billing_rules', data: data, result: true });
    }

    /**
     * 绑定桌台规则
     * @param {*} data 
     * @returns 
     */
    bindTableRules(data) {
        return this.createMultiple({ table: 'game_table_billing_rules', data: data, result: true });
    }

    /**
     * 查询各状态数量
     * @returns 
     */
    getStatusQuantity() {
        const SQL = `SELECT status, COUNT(*) count FROM game_tables GROUP BY status`;
        return this.query(SQL);
    }

    /**
     * 获取表格规则
     * @param {*} param0 
     * @returns 
     */
    getTableRule({ table_id }) {
        const SQL = `
        SELECT 
            r.id,
            r.rule_name,
            r.rule_type,
            r.start_time,
            r.end_time,
            r.min_consumption,
            r.max_duration,
            r.base_fee,
            r.base_hours,
            r.fixed_price,
            r.fixed_duration,
            r.fixed_fee,
            r.weekdays,
            r.priority,
            r.effective_date,
            r.expire_date 
        FROM 
            game_table_billing_rules br 
            LEFT JOIN game_billing_rules r ON br.rule_id = r.id 
        WHERE
            r.tenant_id = '${TenantContext.getCurrentTenant()}' 
            AND br.table_id = '${table_id}' 
            AND r.is_delete = 0
        `;
        return this.query(SQL);
    }
}

module.exports = new TableModel();
